@extends('layouts.app')
@section('title', '内容系统 - 文章列表')
@section('content')
    <div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list"
         style="padding: 20px 30px 0 0;">
        <div class="layui-form-item">
            <label class="layui-form-label">文章分类</label>
            <div class="layui-input-inline">
                <select name="category_id" lay-verify="required">
                    <option value="">请选择</option>
                    @foreach($categories as $category)
                        <option value="{{ $category->id }}"
                                @if(isset($article->category_id)&&$article->category_id==$category->id)selected @endif >{{ $category->name }}</option>
                        @if(isset($category->allChilds)&&!$category->allChilds->isEmpty())
                            @foreach($category->allChilds as $child)
                                <option value="{{ $child->id }}"
                                        @if(isset($article->category_id)&&$article->category_id==$child->id)selected @endif >
                                    &nbsp;&nbsp;&nbsp;┗━━{{ $child->name }}</option>
                                @if(isset($child->allChilds)&&!$child->allChilds->isEmpty())
                                    @foreach($child->allChilds as $third)
                                        <option value="{{ $third->id }}"
                                                @if(isset($article->category_id)&&$article->category_id==$third->id)selected @endif >
                                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;┗━━{{ $third->name }}</option>
                                    @endforeach
                                @endif
                            @endforeach
                        @endif
                    @endforeach
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="" class="layui-form-label">标签</label>
            <div class="layui-input-block">
                @foreach($tags as $tag)
                    <input type="checkbox" name="tags[]" {{ $tag->checked??'' }} value="{{ $tag->id }}"
                           title="{{ $tag->name }}">
                @endforeach
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">文章标题</label>
            <div class="layui-input-inline">
                <input type="text" name="title" value="{{ $article->title ?? old('title') }}" lay-verify="required"
                       placeholder="请输入..." autocomplete="off" class="layui-input">

            </div>
        </div>
        <div class="layui-form-item">
            <label for="" class="layui-form-label">关键词</label>
            <div class="layui-input-block">
                <input type="text" name="keywords" value="{{$article->keywords??old('keywords')}}" placeholder="请输入关键词"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="" class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <textarea name="description" placeholder="请输入描述" style="height: 20px;" autocomplete="off"
                          class="layui-textarea">{{$article->description??old('description')}}</textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">文章封面</label>
            <div class="layui-input-inline">
                <input type="text" name="thumb" placeholder="请选择..." autocomplete="off"
                       value="{{ $article->thumb??'' }}" class="layui-input">
            </div>
            <button style="float: left;" type="button" class="layui-btn" layadmin-event="select_image">选择图片</button>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">文章内容</label>
            <div class="layui-input-inline">
                <!-- 加载编辑器的容器 -->
                <script id="container" name="content" type="text/plain" style="width: 800px;height: 300px;">
                    {!! $article->content??old('content') !!}
                </script>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="" class="layui-form-label">点击量</label>
            <div class="layui-input-block">
                <input type="number" name="click" value="{{$article->click??0}}" lay-verify="required|numeric"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">发布状态</label>
            <div class="layui-input-inline">
                <input type="checkbox" lay-verify="required" lay-filter="status" name="status" lay-skin="switch"
                       lay-text="已发布|待修改">
            </div>
        </div>
        <div class="layui-form-item layui-hide">
            <input type="button" lay-submit lay-filter="LAY-app-form-submit" id="LAY-app-form-submit" value="确认添加">
            <input type="button" lay-submit lay-filter="LAY-app-form-edit" id="LAY-app-form-edit" value="确认编辑">
        </div>
    </div>
@endsection

@section('js')
    <!-- 配置文件 -->
    <script type="text/javascript" src="/static/ueditor/ueditor.config.js"></script>
    <!-- 编辑器源码文件 -->
    <script type="text/javascript" src="/static/ueditor/ueditor.all.js"></script>
    <!-- 实例化编辑器 -->
    <script>
        layui.use(['index', 'form'], function () {
            var $ = layui.$
                , admin = layui.admin
                , form = layui.form;
            var ue = UE.getEditor('container');
            ue.ready(function () {
                ue.execCommand('serverparam', '_token', '{{ csrf_token() }}');
                //设置内容
                //ue.setContent();
            });

            //监听提交
            form.on('submit(LAY-app-form-submit)', function (data) {
                var field = data.field; //获取提交的字段
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                field.content = ue.getContent();//获取内容
                admin.req({
                    url: '/article',
                    type: "post",
                    data: field,
                    success: function (res) {
                        if (res.code === 0) {
                            layer.msg(res.message, {icon: 6});
                        } else {
                            layer.msg(res.message, {icon: 5});
                        }
                    }
                });
                parent.layui.table.reload('LAY-app-content-list'); //重载表格
                parent.layer.close(index); //再执行关闭
            });
        })
    </script>
@endsection
